#include "../common/httplib.h"
#include "CR.hpp"

using namespace httplib;

void Usage(std::string proc)
{
    std::cerr << "Usage: " << "\n\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    DFILESYNC();
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    Server svr;
    svr.Post("/compile_and_run", [](const Request &req, Response &resp)
             {
                DLOG(DEBUG) << "客户端接收到消息啦！开始处理";
        // 用户请求的服务正文是我们想要的json string
        std::string in_json = req.body;
        DLOG(DEBUG) << in_json;
        std::string out_json;
        if(!in_json.empty()){
            CR::handle(in_json, out_json);
            DLOG(DEBUG) << "处理完成，开始返回结果给负载均衡器";

            resp.set_content(out_json, "application/json;charset=utf-8");
        } });

    svr.listen("0.0.0.0", atoi(argv[1])); // 启动http服务
}